clear all
	
*) Bring in Data
	cd "D:\Taylor_JAERE_2020_DONE"				
	insheet using "BagBan_3stores.txt",  delim("|")

	rename v1 store_id
	rename v2 division_id
	rename v3 promo_week_id
	rename v4 upc_id
	rename v5 upc_desc
	rename v6 category_id
	rename v7 hh_id
	rename v8 txn_id
	rename v9 txn_date
	rename v10 txn_time
	rename v11 cashier_nbr
	rename v12 reg_nbr 
	rename v13 item_qty
	rename v14 price
	rename v15 price_paid
	
*)Relabel variables
	label variable store_id "Store ID"
	label variable division_id "Division ID"
	label variable promo_week_id "Promo Week ID"
	label variable upc_id "UPC ID"
	label variable upc_desc "UPC Descr"
	label variable category_id "Category ID"
	label variable hh_id "HH ID"
	label variable txn_id "Txn ID"
	label variable txn_date "Txn Date"
	label variable txn_time "Txn Time"
	label variable cashier_nbr "Cashier Number"
	label variable reg_nbr "Register Number"
	label variable item_qty "Item Qty"
	label variable price "Shelf Price"
	label variable price_paid "Price Paid"

*)Merge in category names
	merge m:1 category_id using map_categ_name.dta
	drop if _merge == 2

*)Create 12 department groups
	replace dept_id = 1 if _merge == 	1 &	((category_id >=3300 & category_id <= 3399) |	(category_id >=8900 & category_id <= 8999))	
	replace dept_id = 2 if _merge == 	1 &	((category_id >=8200 & category_id <= 8399) |	(category_id >=9401 & category_id <= 9599))	
	replace dept_id = 3 if _merge == 	1 &	(category_id >=3601 & category_id <= 3999) 		
	replace dept_id = 4 if _merge == 	1 &	(category_id >=8500 & category_id <= 8599) 		
	replace dept_id = 5 if _merge == 	1 &	(category_id >=8100 & category_id <= 8199) 		
	replace dept_id = 6 if _merge == 	1 &	(category_id >=4001 & category_id <= 4899) 		
	replace dept_id = 7 if _merge == 	1 &	(category_id >=8600 & category_id <= 8899)				
	replace dept_id = 9 if _merge == 	1 &	(category_id >=8400 & category_id <= 8499) 		
	replace dept_id = 10 if _merge == 	1 &	(category_id >=3201 & category_id <= 3299) 		
	replace dept_id = 11 if _merge == 	1 &	(( category_id < 3000) |	(category_id >=9201 & category_id <= 9299) |	(category_id >=9601 & category_id <= 9799) )
	replace dept_id = 12 if _merge == 	1 &	(category_id >=6501 & category_id <= 6599) 						
	replace dept_id = 8 if _merge == 	1 &	dept_id == .									
	tab dept_id, gen(DEPT_)
	label variable DEPT_1 "Txn=Alc & Tobac"
	label variable DEPT_2 "Txn=Bakery & Deli"
	label variable DEPT_3 "Txn=Dairy & Refrig"
	label variable DEPT_4 "Txn=Floral"
	label variable DEPT_5 "Txn=Food Service"
	label variable DEPT_6 "Txn=Frozen"
	label variable DEPT_7 "Txn=Meat & Sea"
	label variable DEPT_8 "Txn=Health & Other"
	label variable DEPT_9 "Txn=Produce"
	label variable DEPT_10 "Txn=Pet"
	label variable DEPT_11 "Txn=Grocery"
	label variable DEPT_12 "Txn=Baby"	
	
				
*)Generate variables for the number of items returned, the amount of expenditures, total scans.
	gen expenditure_paid = price_paid 
	
	gen tot_scan = abs(item_qty)					
	replace tot_scan = 1 if item_qty == 0
	
	drop upc_id upc_desc category_id _merge dept_id
	collapse (sum) item_qty expenditure_paid tot_scan (max) DEPT_*, ///
					by(store_id promo_week_id txn_id txn_date txn_time cashier_nbr reg_nbr) 
		
*) Generate year, hour, min, month variables
	gen str4 year =substr(txn_time,1,4)
	gen str2 hour =substr(txn_time,12,2)
	gen str2 min =substr(txn_time,15,2)
	gen str2 month = substr(txn_time,6, 2)
	gen str2 day = substr(txn_time,9, 2)
	
	destring year, replace
	destring hour, replace
	destring min, replace
	destring month, replace	
	destring day, replace	
									
	tab hour		
	gen tt1 = 0
	replace tt1 = 60 if hour == 12
	replace tt1 = 120 if hour == 13
	replace tt1 = 180 if hour == 14
	replace tt1 = 240 if hour == 15
	replace tt1 = 300 if hour == 16
	replace tt1 = 360 if hour == 17		
	replace tt1 = 420 if hour == 18
	replace tt1 = 480 if hour == 19			
	
	gen tt2 = tt1 + min 
	
	sort store_id year month day reg_nbr cashier_nbr tt2 txn_id
	egen tt3 = group(store_id year month day reg_nbr cashier_nbr)
	bysort tt3: gen txn_mins = tt2- tt2[_n-1]

*) Dont need transaction times for registers that are not regular, express or self-check. 
	replace txn_mins = . if (reg_nbr > 20 & reg_nbr < 51) | (reg_nbr > 54 & reg_nbr < 93) 
	replace txn_mins = . if txn_mins > 20 
	replace txn_mins = . if tot_scan > 200
	replace txn_mins = . if tot_scan > 140 & ((reg_nbr < 55 & reg_nbr > 50) | reg_nbr == 93 | reg_nbr == 94 | reg_nbr == 99)
	replace txn_mins = . if txn_mins > (5 + tot_scan*0.075) & reg_nbr < 21
	replace txn_mins = . if txn_mins > (10 + tot_scan*0.15) & ((reg_nbr < 55 & reg_nbr > 50) | reg_nbr == 93 | reg_nbr == 94 | reg_nbr == 99)

	drop if hour == 19
					
*)Label registers
	gen sc 			= 0
					forvalues xx = 51(1)54 {
					replace sc = 1 if reg_nbr == `xx'
					}
					forvalues xx = 93(1)99 {
					replace sc = 1 if reg_nbr == `xx'
					}		
	
*)For checkout, generate express & non-express
/*Rules of thumb: 4 minute average = Express
			* 10 minute average = Non-Express	*/							
	gen exp_lane 	= 0
	gen n_exp_lane	= 0

	replace exp_lane 	= 1 if store_id == 691 & (reg_nbr < 4) 
	replace n_exp_lane	= 1 if store_id == 691 & (reg_nbr < 21 & reg_nbr > 3)
									
	replace exp_lane 	= 1 if store_id == 955 & (reg_nbr < 3) 
	replace n_exp_lane	= 1 if store_id == 955 & (reg_nbr < 21 & reg_nbr > 2)
																
	replace exp_lane 	= 1 if store_id == 2940 & (reg_nbr < 4 ) 
	replace n_exp_lane	= 1 if store_id == 2940 & (reg_nbr < 11 & reg_nbr > 4)
	
*) Generate counts for the number of transactions per day, number of registers				
	sort store_id year month day reg_nbr hour	
	gen regs_to_keep =  (exp_lane ==1 | n_exp_lane == 1 | sc==1)
	egen dte_shift1 = group(store_id year month day hour regs_to_keep) /*should cashier be in here?*/
	bysort dte_shift1: gen store_dayhour_TRANS = _N	
	drop dte_shift1

	egen dte_shift2 = group(store_id year month day regs_to_keep) /*should cashier be in here?*/
	bysort dte_shift2: gen store_day_TRANS = _N				
	drop dte_shift2
				
*) Generate TREAT & POST		
	gen POST = 0
	gen TREAT = 0	
	replace TREAT = 1 if store_id == 2940	
	replace POST = 1 if (year == 2014 & store_id == 2940)
					
*) Drop transaction with more than 250 scans 
	drop if tot_scan > 250 & (reg_nbr < 21 | (reg_nbr > 50 & reg_nbr < 55) | reg_nbr > 92)
	drop if expenditure_paid > 10000 & (reg_nbr < 21 | (reg_nbr > 50 & reg_nbr < 55) | reg_nbr > 92)
	drop if expenditure_paid < -250 & (reg_nbr < 21 | (reg_nbr > 50 & reg_nbr < 55) | reg_nbr > 92)

*) Drop extra variables
	drop tt1 tt2 tt3 DEPT_8	DEPT_5 regs_to_keep
	drop if txn_mins == .

*)Create date_group
	sort year month day
	egen date_group = group(year month day)
	
*) Create cluster groups	
	egen cluster_group = group(store_id date_group)	
	egen store_reg = group(store_id reg_nbr)
	egen store_cashier = group(store_id cashier_nbr)
	
	
*****************************************************
*) Main regression
*****************************************************	

	gen ln_mins = ln(txn_mins + .01)
	
 *) Policy effect, 3 store sample, scanner data
		reghdfe ln_mins POST tot_scan expenditure_paid DEPT* if n_exp_lane == 1 ///
			, absorb(store_id date_group store_reg store_cashier hour) vce(cluster cluster_group) 
			
		* Appendix Table A.4 *	
		estadd local storefix "Yes", replace 
		estadd local storefix "Yes", replace 
		estadd local datefix "Yes", replace 
		estadd loc se "Robust"
		estimates store REG1
		
		esttab REG1, b(%10.4f) se scalars(N r2 ll) star(* 0.10 ** 0.05 *** 0.01) keep(POST tot_scan expenditure_paid) label
		
		sum txn_mins  if n_exp_lane == 1
		
*****************************************************
*) Do transactions move into other hours of the day?
*****************************************************
	
	
collapse (mean) store_dayhour_TRANS store_day_TRANS, by(store_id date_group POST TREAT hour year)	

*) Number of Transactions
		
	forvalues hh = 11(1)18 {
		preserve
			keep if hour == `hh'
			reghdfe store_dayhour_TRANS POST, absorb(store_id date_group) vce(robust)
				estimates store REG`hh'
		restore
}

preserve
drop if hour == 11 | hour ==12 | hour == 16 | hour == 17 | hour == 18
collapse (sum) store_dayhour_TRANS (mean) store_day_TRANS, by(store_id date_group POST TREAT year)	

			reghdfe store_dayhour_TRANS POST, absorb(store_id date_grou) vce(robust)
				estimates store REG00	

			reghdfe store_day_TRANS POST, absorb(store_id date_group) vce(robust)
				estimates store REG000
restore

coefplot    (REG11, rename(POST = "11am") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///
			(REG12, rename(POST = "12pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///
			(REG13, rename(POST = "1pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///
			(REG14, rename(POST = "2pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///			
			(REG15, rename(POST = "3pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///
			(REG16, rename(POST = "4pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///
			(REG17, rename(POST = "5pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)) ///
			(REG18, rename(POST = "6pm") m(circle) mc(cranberry) mlc(cranberry) ciopts(lc(cranberry)) msize(large)), ///			
					yline(0) vertical xsize(6) ysize(3) levels(90) scheme(s1mono)  nooffsets keep(POST) ///
					ytitle(Diff. in # of Txns. Processed, size(huge)) ylabel(-200(50)100,  angle(0) labsize(huge)) ///
					legend(off) saving(eventStudy1, replace) xlabel( , labsize(huge))	
			
coefplot    (REG00, rename(POST = "1-4pm") m(circle) mc(white) mlc(blue) ciopts(lc(blue)) msize(large)) ///
			(REG000, rename(POST = "All Day") m(circle) mc(white) mlc(blue) ciopts(lc(blue)) msize(large)), ///			
					yline(0) vertical xsize(4) ysize(3) levels(90) scheme(s1mono)  nooffsets keep(POST) ///
			ylabel(-200(50)100,  angle(0) labsize(huge)) ///
			legend(off) saving(eventStudy2, replace) xlabel( , labsize(huge))					
			
		graph bar (mean) store_dayhour_TRANS if TREAT ==1 & POST ==0, over(hour, relabel(1 "11am" 2 "12pm" 3 "1pm" 4 "2pm" 5 "3pm" 6 "4pm" 7 "5pm" 8 "6pm") label(labsize(huge))) ///
		legend(off) scheme(s1mono) xsize(6) ysize(3) fysize(30) fxsize(190) ///
		ytitle("    "  " " "Txns. per Hour" "(Pre-policy)", size(huge)) outergap(.9) bargap(150) ///
		saving(storeBar, replace)  intensity(25) exclude0 ylabel(150(25)225, angle(0) labsize(huge))
			graph export "BarGraph_3STORE.pdf", as(pdf) replace
			
		gr combine eventStudy1.gph eventStudy2.gph storeBar.gph, imargin(0 0 0 0) scheme(s1mono) ///
			xcom iscale(*.75) hole(4) xsize(7) ysize(3) 

		graph export "Fig6_ShiftingHour.pdf", as(pdf) replace	
			

		

								
	
